package com.wtx.fitness.service.impl;

import com.wtx.fitness.entity.SportItem;
import com.wtx.fitness.mapper.ScoreMapper;
import com.wtx.fitness.mapper.SportItemMapper;
import com.wtx.fitness.service.SportItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 体测项目服务实现类
 */
@Service
public class SportItemServiceImpl implements SportItemService {

  @Autowired
  private SportItemMapper sportItemMapper;

  @Autowired
  private ScoreMapper scoreMapper;

  @Override
  public List<SportItem> getAllSportItems() {
    return sportItemMapper.selectAll();
  }

  @Override
  public SportItem getSportItemById(Integer id) {
    return sportItemMapper.selectById(id);
  }

  @Override
  public List<SportItem> getSportItemsByGender(String gender) {
    return sportItemMapper.selectByGender(gender);
  }

  @Override
  public int addSportItem(SportItem sportItem) {
    return sportItemMapper.insert(sportItem);
  }

  @Override
  public int updateSportItem(SportItem sportItem) {
    return sportItemMapper.update(sportItem);
  }

  @Override
  @Transactional
  public int deleteSportItem(Integer id) {
    // 先删除与该体测项目关联的所有成绩记录
    scoreMapper.deleteByItemId(id);

    // 然后删除体测项目
    return sportItemMapper.deleteById(id);
  }
}